import request from './request';

export function getMemberList(params) {
  return request({ url: '/api/v1/members', method: 'get', params });
}
export function addMember(data) {
  return request({ url: '/api/v1/members', method: 'post', data });
}
export function updateMember(data) {
  return request({ url: `/api/v1/members/${data.id}`, method: 'put', data });
}
export function deleteMember(id) {
  return request({ url: `/api/v1/members/${id}`, method: 'delete' });
}
// 获取成员详情
export function getMemberDetail(id) {
  return request({
    url: `/api/v1/members/${id}`,
    method: 'get'
  })
}

// 获取成员的子女列表（按排序）
export function getMemberChildren(id) {
  return request({
    url: `/api/v1/members/${id}/children`,
    method: 'get'
  })
}

// 更新子女排序
export function updateChildOrder(id, childOrder) {
  return request({
    url: `/api/v1/members/${id}/child-order`,
    method: 'put',
    params: { childOrder }
  })
}

// 重新排序同一父母下的所有子女
export function reorderChildren(parentId, childIds) {
  return request({
    url: `/api/v1/members/${parentId}/children/reorder`,
    method: 'put',
    data: childIds
  })
}

// 获取树形结构的子节点（懒加载）
export function getTreeChildren(parentId) {
  return request({
    url: '/api/v1/members/tree/children',
    method: 'get',
    params: { parentId }
  })
}

// 搜索树形结构中的成员
export function searchTreeByName(name) {
  return request({
    url: '/api/v1/members/tree/search',
    method: 'get',
    params: { name }
  })
} 

// 新增：配偶管理API
export function getMemberSpouses(memberId) {
  return request({
    url: `/api/v1/members/${memberId}/spouses`,
    method: 'get'
  })
}

export function addMemberSpouse(spouseVO) {
  return request({
    url: '/api/v1/members/spouses',
    method: 'post',
    data: spouseVO
  })
}

export function updateMemberSpouse(spouseId, spouseVO) {
  return request({
    url: `/api/v1/members/spouses/${spouseId}`,
    method: 'put',
    data: spouseVO
  })
}

export function deleteMemberSpouse(spouseId) {
  return request({
    url: `/api/v1/members/spouses/${spouseId}`,
    method: 'delete'
  })
}

// 新增：增强的树形结构API（包含配偶信息）
export function getEnhancedTreeChildren(parentId, branchId) {
  return request({
    url: '/api/v1/members/tree/enhanced/children',
    method: 'get',
    params: { parentId, branchId }
  })
}

export function searchEnhancedTreeByName(name) {
  return request({
    url: '/api/v1/members/tree/enhanced/search',
    method: 'get',
    params: { name }
  })
}

// 新增：统计API
export function getStatistics() {
  return request({
    url: '/api/v1/members/statistics',
    method: 'get'
  })
} 

// 新增：拖拽更新父节点关系（返回完整响应对象）
export function updateMemberParent(memberId, newParentId) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open('PUT', `/api/v1/members/${memberId}/parent?newParentId=${newParentId}`, true);
    
    // 添加认证头
    const token = localStorage.getItem('token');
    if (token) {
      xhr.setRequestHeader('Authorization', 'Bearer ' + token);
    }
    xhr.setRequestHeader('Content-Type', 'application/json');
    
    xhr.onload = function() {
      if (xhr.status === 200) {
        try {
          const response = JSON.parse(xhr.responseText);
          resolve(response);
        } catch (e) {
          reject(new Error('Invalid JSON response'));
        }
      } else {
        reject(new Error(`HTTP ${xhr.status}: ${xhr.statusText}`));
      }
    };
    
    xhr.onerror = function() {
      reject(new Error('Network error'));
    };
    
    xhr.send();
  });
} 